//
//  KSOptionMenu.m
//  Dingdong
//
//  Created by 程强 on 16/11/22.
//  Copyright © 2016年 程强. All rights reserved.
//

#import "KSOptionMenu.h"

@interface KSOptionMenu()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) UITableView *menuTable;
@end

@implementation KSOptionMenu
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [backBtn setFrame:frame];
        backBtn.backgroundColor = [[UIColor darkGrayColor]colorWithAlphaComponent:0.6];
        [backBtn addTarget:self action:@selector(removeMenu:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:backBtn];
        
        self.menuTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height) style:UITableViewStylePlain];
        _menuTable.delegate = self;
        _menuTable.dataSource = self;
        [self addSubview:_menuTable];
        _menuTable.layer.cornerRadius = 10.0;
        _menuTable.clipsToBounds = YES;
        _menuTable.scrollEnabled = NO;
        
        [_menuTable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.centerY.equalTo(self);
            make.width.equalTo(self).offset(-70);
            make.height.equalTo(@(44*4));
        }];
        self.alpha = 0;
        
        [UIView animateWithDuration:0.3 animations:^{
            self.alpha = 1.0;
        } completion:^(BOOL finished) {
            
        }];
        
        [KKAnimationKit animationTimeInterval:0.2 scaleFrom:0.5 to:1.0 inView:_menuTable autoreverse:NO];
        
    }
    return self;
}
- (void)removeMenu:(UIButton *)btn
{
    [self removeFromSuperview];
}
- (void)setDataAry:(NSArray *)dataAry
{
    _dataAry = dataAry;
    [_menuTable reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataAry.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44.0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *indentifier = @"MenuCell";
    
    KSOptionMenuCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    
    if (!cell) {
        cell = [[KSOptionMenuCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier];
    }
    cell.menuLbl.text = _dataAry[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
@end


@implementation KSOptionMenuCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.menuLbl = [[KKLabel alloc]initWithBlock:^(KKLabel *lbl) {
            lbl.laText(@"",[UIColor blackColor],[UIFont systemFontOfSize:15.0],NSTextAlignmentLeft);
        }];
        [self.contentView addSubview:_menuLbl];
        [_menuLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.contentView);
            make.left.equalTo(self.contentView).offset(15);
            make.height.equalTo(@30);
            make.width.equalTo(@150);
        }];
        
        self.selectImage = [[UIImageView alloc]init];
        [self.contentView addSubview:_selectImage];
        [_selectImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.contentView);
            make.right.equalTo(self.contentView).offset(20);
            make.width.height.equalTo(@30);
        }];
    }
    return self;
}

@end
